class Graph<T> {
  //顶点
  private vertex: T[] = [];

  //边，邻接表
  private adjList: Map<T, T[]> = new Map();

  /* 添加顶点和边的方法 */
  addVertex(vertex: T) {
    this.vertex.push(vertex);
    this.adjList.set(vertex, []);
  }

  addEdge(vertex1: T, vertex2: T) {
    this.adjList.get(vertex1)?.push(vertex2);
    this.adjList.get(vertex2)?.push(vertex1);
  }

  traverse() {
    console.log("Graph");
    this.vertex.map(v => {
      const edges = this.adjList.get(v);
      console.log(`${v} -> ${edges?.join(" ")}`);
    });
  }
}

const graph = new Graph<string>();

for (let i = 65; i < 74; i++) {
  graph.addVertex(String.fromCharCode(i));
}

graph.addEdge("A", "B");
graph.addEdge("A", "C");
graph.addEdge("A", "D");
graph.addEdge("C", "D");
graph.addEdge("C", "G");
graph.addEdge("D", "G");
graph.addEdge("D", "H");
graph.addEdge("B", "E");
graph.addEdge("B", "F");
graph.addEdge("E", "I");

graph.traverse();

export {};
